<script setup>
import { onMounted, ref, reactive } from 'vue'
import { getLogPageListService } from '@/api/log/log'
import { getLogById } from '@/api/log/log'
import { ElMessage } from 'element-plus'

const logPageList = ref([])
const page = ref(1)
const pageSize = ref(8)
const total = ref(0)

const OBJECT_EMPLOYEE = 0
const OBJECT_FOOD = 1
const OBJECT_FOODTYPE = 2
const OBJECT_PATIENTRECIPE = 3

const updateTime = ref(null)
const createTime = ref(null)
const objectType = ref(-1)
const newObject = ref([])
const oldObject = ref([])
const innerTableData = ref([])
const classData = reactive({
  title: '日志详情',
  dialogVisible: false,
  id: ''
})
const queryParams = ref({
  page: 1,
  pageSize: 8,
  name: null
})
const getLogPageList = async () => {
  const res = await getLogPageListService(queryParams.value)
  logPageList.value = res.data.data.records
  total.value = res.data.data.total
}

const getLog = async (row) => {
  await getLogById(row)
    .then((res) => {
      if (String(res.data.code) === '1') {
        objectType.value = res.data.data.objectType
        newObject.value = JSON.parse(res.data.data.newObject)
        oldObject.value = JSON.parse(res.data.data.oldObject)
        innerTableData.value.push(newObject.value)
        innerTableData.value.push(oldObject.value)
        if (res.data.data.objectType === OBJECT_FOODTYPE)
          updateTime.value = createTime.value = res.data.data.createTime
        if (innerTableData.value)
          innerTableData.value = innerTableData.value.filter(
            (item) => item !== null
          )
        classData.dialogVisible = true
      }
    })
    .catch((err) => {
      ElMessage('请求出错了：' + err)
      // this.$message.error('请求出错了：' + err)
    })
}

const handleBeforeClose = () => {
  innerTableData.value = []
  objectType.value = -1
  updateTime.value = null
  createTime.value = null
  classData.dialogVisible = false
}

const handleQuery = () => {
  queryParams.value.page = 1
  getLogPageList()
}
const resetForm = () => {
  queryParams.value.name = null
  handleQuery()
}
//在页面初次渲染执行
onMounted(() => {
  getLogPageList()
})
</script>

<template>
  <el-dialog
    v-model="classData.dialogVisible"
    v-model:visible="classData.dialogVisible"
    width="75%"
    :title="classData.title"
    :modal="true"
    :before-close="handleBeforeClose"
    style="border-radius: 25px"
  >
    <!--0 修改-->
    <template v-if="objectType === OBJECT_EMPLOYEE">
      <el-table :data="innerTableData" stripe>
        <el-table-column
          prop="name"
          label="员工姓名"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="账号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          align="center"
        ></el-table-column>
      </el-table>
    </template>
    <!--    1-->
    <template v-else-if="objectType === OBJECT_FOOD">
      <el-table :data="innerTableData" stripe style="width: 100%">
        <el-table-column
          v-if="name !== null"
          prop="name"
          label="食物名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="energy"
          label="能量"
          align="center"
        ></el-table-column>
        <el-table-column prop="carbohydrate" label="碳水" align="center">
        </el-table-column>
        <el-table-column prop="protein" label="蛋白质" align="center">
        </el-table-column>
        <el-table-column
          prop="fat"
          label="脂肪"
          align="center"
        ></el-table-column>
      </el-table>
    </template>
    <!--    2 增加0 删除1-->
    <template v-else-if="objectType === OBJECT_FOODTYPE">
      <el-table :data="innerTableData" stripe style="width: 100%">
        <el-table-column
          prop="name"
          label="分类名称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="分类类型"
          align="center"
        ></el-table-column>
        <el-table-column prop="updateTime" label="最后操作时间" align="center">
          <template v-slot="scope">
            <span v-if="scope.row.updateTime !== undefined">{{
              scope.row.updateTime
            }}</span>
            <span v-else>{{ updateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template v-slot="scope">
            <span v-if="scope.row.createTime !== undefined">{{
              scope.row.createTime
            }}</span>
            <span v-else>{{ createTime }}</span>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--    3 -->
    <template v-else-if="objectType === OBJECT_PATIENTRECIPE">
      <el-table :data="innerTableData" stripe style="width: 100%">
        <el-table-column
          prop="id"
          label="食谱ID"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="grains"
          label="谷物组"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="grainsWeight"
          label="谷物组重量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="vegetable"
          label="蔬菜组"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="vegetableWeight"
          label="蔬菜组重量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="meat"
          label="肉组"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="meatWeight"
          label="肉组重量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="fruit"
          label="水果组"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="fruitWeight"
          label="水果组重量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="milkGroup"
          label="乳类组"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="milkGroupWeight"
          label="乳类组重量"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="grease"
          label="脂肪组"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="greaseWeight"
          label="脂肪组重量"
          align="center"
        ></el-table-column>
      </el-table>
    </template>
  </el-dialog>

  <div class="floating-box">
    <div>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        label-width="68px"
        class="no-wrap"
        @keydown.enter.prevent
      >
        <el-form-item label="用户名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入用户姓名"
            prefix-icon="Search"
            style="width: 200px"
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item>
          <div class="no-wrap">
            <el-button
              icon="Search"
              plain
              round
              color="#95d475"
              @click="handleQuery()"
              >搜索</el-button
            >
            <el-button
              icon="Refresh"
              plain
              round
              color="#95d475"
              @click="resetForm()"
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-form>
      <el-divider />
    </div>
    <el-table :data="logPageList" width="100%" :stripe="true" size="large">
      <el-table-column
        label="用户名"
        prop="username"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="操作"
        prop="actionDesc"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="描述"
        prop="description"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="时间"
        prop="createTime"
        align="center"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="操作" prop="" align="center">
        <template #default="{ row }">
          <el-button
            type="primary"
            color="#8fbc8f"
            plain
            round
            icon="View"
            @click="getLog(row)"
            >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="getLogPageList"
        @current-change="getLogPageList"
      />
    </div>
  </div>
</template>

<style scoped>
/deep/ .el-input__wrapper {
  border-radius: 50px;
  /*background-color: red;*/
}
</style>
